#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os, os.path, sys

import tempfile
import unittest

from foxglove import log

from foxglove.cellstore import KeySmith

class   TestKeySmith(unittest.TestCase) :
    def test_assemble(self) :
        key = 'mykey'
        operation = KeySmith.OP_INSERT
        expire = 12345
        timestamp = 900000
        keystring = 'mykey %X 00 %X' % (timestamp, timestamp + expire)

        smith = KeySmith()
        _keystring = smith.assemble(key, operation, expire, timestamp)

        self.assertEqual(keystring, _keystring)
        self.assertEqual(keystring, smith.keystring)
        self.assertEqual(key, smith.key)
        self.assertEqual(operation, smith.operation)
        self.assertEqual(timestamp, smith.timestamp)
        self.assertEqual(expire + timestamp, smith.expire)

        key = 'mykey'
        operation = KeySmith.OP_INSERT
        expire = 0
        timestamp = 900000
        keystring = 'mykey 0 00 0'

        smith = KeySmith()
        _keystring = smith.assemble(key, operation, expire, timestamp)

        self.assertEqual(keystring, _keystring)
        self.assertEqual(keystring, smith.keystring)
        self.assertEqual(key, smith.key)
        self.assertEqual(operation, smith.operation)
        self.assertNotEqual(timestamp, smith.timestamp)
        self.assertEqual(0, smith.timestamp)
        self.assertNotEqual(expire + timestamp, smith.expire)
        self.assertEqual(0, smith.expire)

        # test_assemble()

    def test_disemble(self) :
        key = 'mykey'
        operation = KeySmith.OP_ERASE
        expire = 12345
        timestamp = 800000
        keystring = 'mykey %X FF %X' % (timestamp, timestamp + expire)

        smith = KeySmith()
        smith.desemble(keystring)

        self.assertEqual(keystring, smith.keystring)
        self.assertEqual(key, smith.key)
        self.assertEqual(operation, smith.operation)
        self.assertEqual(timestamp, smith.timestamp)
        self.assertEqual(expire + timestamp, smith.expire)

        key = 'mykey'
        operation = KeySmith.OP_ERASE
        expire = 0
        timestamp = 800000
        keystring = 'mykey 0 FF 0'

        smith = KeySmith()
        smith.desemble(keystring)

        self.assertEqual(keystring, smith.keystring)
        self.assertEqual(key, smith.key)
        self.assertEqual(operation, smith.operation)
        self.assertNotEqual(timestamp, smith.timestamp)
        self.assertEqual(0, smith.timestamp)
        self.assertNotEqual(expire + timestamp, smith.expire)
        self.assertEqual(0, smith.expire)
        # test_disemble()

    # class TestKeySmith

#######################################################################

def test_suite():
    suite = unittest.TestSuite()

    suite.addTest(unittest.makeSuite(TestKeySmith))

    return suite

if  __name__ == '__main__' :
    #log.setLogPath('/dev/null')
    log.enableDebug()

    unittest.main()
